const Koa = require('koa')
const consola = require('consola')
const { Nuxt, Builder } = require('nuxt')

// 自定义配置
const mongoose = require('mongoose')
const bodyParser = require('koa-bodyparser')
const session = require('koa-generic-session')
const Redis = require('koa-redis')
const json = require('koa-json')
const dbConfig = require('./dbs/config.js')
const passport = require('./interface/utils/passport.js')
const users = require('./interface/users.js')
const geo = require('./interface/geo.js')
const search = require('./interface/search.js')
const categroy = require('./interface/categroy.js')
const cart = require('./interface/cart.js')
const order = require('./interface/order.js')

const app = new Koa()

// Import and Set Nuxt.js options
const config = require('../nuxt.config.js')
config.dev = app.env !== 'production'

async function start () {
  // Instantiate nuxt.js
  const nuxt = new Nuxt(config)

  const {
    host = process.env.HOST || '127.0.0.1',
    port = process.env.PORT || 3000
  } = nuxt.options.server

  // 自定义配置
  app.keys = ['mt', 'keyskeys']
  app.proxy = true
  app.use(session({key: 'mt', prefix: 'mt:uid', store: new Redis()}))
  app.use(bodyParser({
    extendTypes:['json','form','text']
  }))
  app.use(json())
  mongoose.set('useCreateIndex', true)
  mongoose.connect(dbConfig.dbs,{
    useNewUrlParser: true,
    useUnifiedTopology: true 
  })
  app.use(passport.initialize())
  app.use(passport.session())

  await nuxt.ready()
  // Build in development
  if (config.dev) {
    const builder = new Builder(nuxt)
    await builder.build()
  }

  // 自定义配置
  app.use(users.routes()).use(users.allowedMethods())
  app.use(geo.routes()).use(geo.allowedMethods())
  app.use(search.routes()).use(search.allowedMethods())
  app.use(categroy.routes()).use(categroy.allowedMethods())
  app.use(cart.routes()).use(cart.allowedMethods())
  app.use(order.routes()).use(order.allowedMethods())


  app.use((ctx) => {
    ctx.status = 200
    ctx.respond = false // Bypass Koa's built-in response handling
    ctx.req.ctx = ctx // This might be useful later on, e.g. in nuxtServerInit or with nuxt-stash
    nuxt.render(ctx.req, ctx.res)
  })

  app.listen(port, host)
  consola.ready({
    message: `Server listening on http://${host}:${port}`,
    badge: true
  })
}

start()
